#define _CRT_SECURE_NO_WARNINGS 1

#include <string>
using namespace std;

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        string s1;
        string s2;
        for (auto ch : s)
        {
            if (!s1.empty() && ch == '#') s1.pop_back();
            else if (ch != '#')s1 += ch;
        }
        for (auto ch : t)
        {
            if (!s2.empty() && ch == '#') s2.pop_back();
            else if (ch != '#')s2 += ch;
        }   
        return s1 == s2;
    }
};